//
//  MYTableView.m
//  kunyuantang
//
//  Created by xiepeng on 2018/4/25.
//  Copyright © 2018年 dev. All rights reserved.
//

#import "MYTableView.h"

@interface MYTableView ()<DZNEmptyDataSetSource, DZNEmptyDataSetDelegate>
@property (nonatomic,strong) NSMutableArray *refreshImages;//刷新动画的图片数组
@property (nonatomic,strong) NSMutableArray *normalImages;//普通状态下的图片数组
@property (nonatomic , copy)  NSString  *tipText;
@property (nonatomic , strong)  UIImage  *tipImage;
@property (nonatomic , assign)  CGFloat  tableViewHeight;
@end

@implementation MYTableView
-(instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style {
    self = [super initWithFrame:frame style:style];
    if (self) {
        [self setTableViewInfo];
    }
    return self;
}

- (void)setTableViewInfo {
    if (@available(iOS 11.0, *)) {
        self.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        self.scrollIndicatorInsets = self.contentInset;
    }
    
    self.nodataView = [[MYNoDataView alloc] initWithFrame:self.bounds];
    self.nodataView.userInteractionEnabled = YES;
    self.nodataView.backgroundColor = UIColor.whiteColor;
    
    self.estimatedRowHeight = 40;
    self.backgroundColor = [UIColor clearColor];
    self.emptyDataSetSource = self;
    self.emptyDataSetDelegate = self;
    self.noDataType = TableViewNoDataType_Data;
    self.tipText = @"暂无数据";
    self.tipImage = [UIImage imageNamed:@"nodata_data"];
    self.rowHeight = UITableViewAutomaticDimension;
    self.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self setNoDataType:TableViewNoDataType_Data];
    
//    MJRefreshGifHeader *header = [MJRefreshGifHeader headerWithRefreshingTarget:self refreshingAction:@selector(tableViewRefreshData)];
//    [header setImages:self.refreshImages forState:MJRefreshStateRefreshing];
//    [header setImages:self.normalImages forState:MJRefreshStateIdle];
//    [header setImages:self.refreshImages forState:MJRefreshStatePulling];
//    header.lastUpdatedTimeLabel.hidden= YES;//如果不隐藏这个会默认 图片在最左边不是在中间
//    header.stateLabel.hidden = YES;
//    self.mj_header = header;
    

    
    [self addMJHeader];
    [self addMJFooter];
    
    self.showsVerticalScrollIndicator = NO;
}
- (void)setNoDataType:(NoDataType)noDataType {
    _noDataType = noDataType;
    [_nodataView nodataImage];
    self.tipText = @"暂无数据";
    self.tipImage = [UIImage imageNamed:@"file_index_nodata"];
//    if (_noDataType == TableViewNoDataType_NOLOGINFILE) {
//        self.tipText = @"你还没有登录，请登录后查看！";
//        self.tipImage = [UIImage imageNamed:@"file_index_nodata"];
//    }else if (_noDataType == TableViewNoDataType_FILE) {
//        self.tipText = @"暂时没有文件，添加吧~";
//        self.tipImage = [UIImage imageNamed:@"file_index_nodata"];
//    }else if (_noDataType == TableViewNoDataType_VIDEO) {
//        self.tipText = @"暂时没有视频，添加吧~";
//        self.tipImage = [UIImage imageNamed:@"s_video_null"];
//    }else if (_noDataType == TableViewNoDataType_AUDIO) {
//        self.tipText = @"暂时没有音频，添加吧~";
//        self.tipImage = [UIImage imageNamed:@"s_audio_null"];
//    }else if (_noDataType == TableViewNoDataType_Note) {
//        self.tipText = @"暂时没有笔记，添加吧~";
//        self.tipImage = [UIImage imageNamed:@"s_note_null"];
//    }else  if (_noDataType == TableViewNoDataType_Message) {
//        self.tipText = @"暂时没有加密短信哦~";
//        self.tipImage = [UIImage imageNamed:@"m_index_null"];
//    }else if (_noDataType == TableViewNoDataType_Data) {
//        self.tipText = @"暂无数据";
//        self.tipImage = [UIImage imageNamed:@"file_index_nodata"];
//    }
    [_nodataView setHaveHeaderView:self.tableHeaderView.height];
    self.nodataView.tipLabel.text = self.tipText;
    self.nodataView.nodataImage.image = self.tipImage;
}

- (void)setTableHeaderView:(UIView *)tableHeaderView {
    [super setTableHeaderView:tableHeaderView];
    
    self.tableViewHeight = tableHeaderView.height;
    
//    if (self.noDataType == TableViewNoDataType_NoDataBottom || self.noDataType == TableViewNoDataType_GuanZhu) {
//        [_nodataView setHaveHeaderView:self.tableHeaderView.height];
//    }
}


- (void)emptyDataSet:(UIScrollView *)scrollView didTapView:(UIView *)view {
    NSLog(@"点击了-----%@",view);
}

- (UIColor *)backgroundColorForEmptyDataSet:(UIScrollView *)scrollView{
    return UIColor.whiteColor;
}


- (UIView *)customViewForEmptyDataSet:(UIScrollView *)scrollView {
////
//////    view.clipsToBounds = true;
////
////
//////    NSString *height = [NSString stringWithFormat:@"H:[view(%0.0f)]",0.01];
////
////    // 高度
//////    NSString *height2 = @"H:[view(10.0)]";
//////    NSString *top = [NSString stringWithFormat:@""];
////    NSString *height = [NSString stringWithFormat:@"V:|-100-[view(375.0)]"];
//////
////    [view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:height options:0 metrics:nil views:NSDictionaryOfVariableBindings(view)]];
//////    [view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:height2 options:0 metrics:nil views:NSDictionaryOfVariableBindings(view)]];
//////    [view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:weight options:0 metrics:nil views:NSDictionaryOfVariableBindings(view)]];
    return self.nodataView;
}

- (BOOL)emptyDataSetShouldAllowScroll:(UIScrollView *)scrollView{
    return YES;
}

//- (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView {
//
//        return self.tipImage;
//}
////- (UIImage *)buttonImageForEmptyDataSet:(UIScrollView *)scrollView forState:(UIControlState)state {
////    return [UIImage imageNamed:@"nodata_refresh"];
////}
//
//- (void)emptyDataSet:(UIScrollView *)scrollView didTapButton:(UIButton *)button {
////    [self refreshDataWithTabelViewTag:0];
//}
//
//- (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView {
//    NSString *title = @"";
//    NSDictionary *attributes = @{
//                                 NSFontAttributeName:MY_SCALE_RegularFONT(16),
//                                 NSForegroundColorAttributeName:MyColor(@"#646566")
//                                 };
//    return [[NSAttributedString alloc] initWithString:title attributes:attributes];
//}


- (void)addMJHeader {
    self.mj_header = nil;
    MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(tableViewRefreshData)];
    self.mj_header = header;
}
- (void)addMJFooter {
    self.mj_footer = nil;
    MJRefreshBackNormalFooter*footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(tableViewLoadMoreData)];
    self.mj_footer = footer;
    [footer setTitle:@"正在加载更多数据" forState:MJRefreshStateRefreshing];
}

- (void)tableViewRefreshData {
    [self.mj_header endRefreshing];
    if (self.refreshDataBlock) {
        self.refreshDataBlock();
    }
    
    if ([self.dataChangeDelegate respondsToSelector:@selector(refreshDataWithTabelViewTag:)]) {
        [self.dataChangeDelegate refreshDataWithTabelViewTag:self.tag];
    }
}

- (void)tableViewLoadMoreData {
     [self.mj_footer endRefreshing];
    if (self.loadMoreDataBlock) {
        self.loadMoreDataBlock();
    }
    if ([self.dataChangeDelegate respondsToSelector:@selector(loadMoreDataWithTabelViewTag:)]) {
        [self.dataChangeDelegate loadMoreDataWithTabelViewTag:self.tag];
    }
}

- (NSMutableArray *)refreshImages
{
    if (_refreshImages == nil) {
        _refreshImages = [[NSMutableArray alloc] init];
        //循环添加图片
        for (int i=0; i<13; i++) {
            //logoxu0
            UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"logo_%d", i]];
            
            [self.refreshImages addObject:image];
        }
    }
    return _refreshImages;
}
- (NSMutableArray *)normalImages
{
    if (_normalImages == nil) {
        _normalImages = [[NSMutableArray alloc] init];
        UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"logo_0.png"]];
        [self.normalImages addObject:image];
    }
    return _normalImages;
}


- (void)removeMJRefresh {
    self.mj_header = nil;
    self.mj_footer = nil;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
